package com.ruoyi.system.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysDeptToken;
import com.ruoyi.system.mapper.SysDeptTokenMapper;
import com.ruoyi.system.service.ISysDeptTokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 【部门token】Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-05-13
 */
@Service
public class SysDeptTokenServiceImpl implements ISysDeptTokenService 
{
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private SysDeptTokenMapper sysDeptTokenMapper;

    @Resource
    private RedisCache redisCache;

    /**
     * 查询【部门token】
     * 
     * @param id 【部门token】主键
     * @return 【部门token】
     */
    @Override
    public SysDeptToken selectSysDeptTokenById(Long id)
    {
        return sysDeptTokenMapper.selectSysDeptTokenById(id);
    }

    /**
     * 查询【部门token】列表
     * 
     * @param sysDeptToken 【部门token】
     * @return 【部门token】
     */
    @Override
    public List<SysDeptToken> selectSysDeptTokenList(SysDeptToken sysDeptToken)
    {
        return sysDeptTokenMapper.selectSysDeptTokenList(sysDeptToken);
    }

    /**
     * 新增【部门token】
     * 
     * @param sysDeptToken 【部门token】
     * @return 结果
     */
    @Override
    @SuppressWarnings("all")
    public int insertSysDeptToken(SysDeptToken sysDeptToken)
    {
        int row = sysDeptTokenMapper.insertSysDeptToken(sysDeptToken);
        if(row > 0){
            if(sysDeptToken.getDeptId() != null){
                redisCache.setCacheObject(getCacheKey(sysDeptToken.getDeptId().toString()),sysDeptToken);
            }

         }
        return row;
    }

    /**
     * 修改【部门token】
     * 
     * @param sysDeptToken 【部门token】
     * @return 结果
     */
    @Override
    @SuppressWarnings("all")
    public int updateSysDeptToken(SysDeptToken sysDeptToken)
    {
       int row =  sysDeptTokenMapper.updateSysDeptToken(sysDeptToken);
       if(row > 0){
           if(sysDeptToken.getDeptId() != null){
               redisCache.setCacheObject(getCacheKey(sysDeptToken.getDeptId().toString()),sysDeptToken);
           }

       }
        return row;
    }

    /**
     * 批量删除【部门token】
     * 
     * @param ids 需要删除的【部门token】主键
     * @return 结果
     */
    @Override
    @SuppressWarnings("all")
    public int deleteSysDeptTokenByIds(Long[] ids)
    {
        List<SysDeptToken> sysTokenByIds = sysDeptTokenMapper.getSysTokenByIds(ids);
        int row = sysDeptTokenMapper.deleteSysDeptTokenByIds(ids);
        if(row > 0 ){
            sysTokenByIds.stream().forEach(item -> {
                if(item.getDeptId() != null){
                    redisCache.deleteObject(getCacheKey(item.getDeptId().toString()));
                }

            });
        }
        return row;
    }

    /**
     * 删除【部门token】信息
     * 
     * @param id 【部门token】主键
     * @return 结果
     */
    @Override
    @SuppressWarnings("all")
    public int deleteSysDeptTokenById(Long id)
    {
        SysDeptToken sysDeptToken = sysDeptTokenMapper.selectSysDeptTokenById(id);

        int row = sysDeptTokenMapper.deleteSysDeptTokenById(id);
        if(row > 0){
            if(sysDeptToken.getDeptId() != null){
                redisCache.deleteObject(getCacheKey(sysDeptToken.getDeptId().toString()));
            }

        }
        return row;
    }

    @Override
    public SysDeptToken listByDeptId(SysDeptToken sysDeptToken) {
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            Long mchId = loginUser.getUser().getMchId();
            sysDeptToken.setDeptId(mchId);
            SysDeptToken sysDeptToken1 = redisCache.getCacheObject(getCacheKey(mchId.toString()));
            if(Objects.nonNull(sysDeptToken1)){
                log.info("从缓存中获取 部门token sysDeptToken1 ={}", JSONObject.toJSONString(sysDeptToken1));
                return sysDeptToken1;
            }

        } catch (Exception e) {
            log.info("根据部门id获取天润token异常 exception = {}", e);
        }
        return sysDeptTokenMapper.listByDeptId(sysDeptToken);

    }

    @Override
    public SysDeptToken getSysDeptTokenByEnterpriseId(String enterpriseId) {

        return sysDeptTokenMapper.getSysDeptTokenByEnterpriseId(enterpriseId);
    }


    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey)
    {
        return Constants.SYS_TOKEN_CONFIG_KEY + configKey;
    }
}
